namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Web.Mvc;
    using System.Web.Routing;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web.Routing;

    [InternalServerError]
    [Allow(Methods = "GET, HEAD, OPTIONS")]
    [CacheControl, ContentLanguage, ContentLocation, ContentMD5]
    public sealed class SiteMapXmlController : Controller, IRegisterRoutes
    {
        private static readonly AbsoluteUri RecordUrn = "urn://xml/sitemap";

        void IRegisterRoutes.Register(RouteCollection routes)
        {
            if (null == routes)
            {
                throw new ArgumentNullException("routes");
            }

            routes.MapRoute(
                "GET sitemap.xml",
                "sitemap.xml",
                new { controller = "SiteMapXml", action = "XmlRepresentation" },
                new { method = new HttpMethodConstraint("GET") });

            routes.MapRoute(
                "HEAD sitemap.xml",
                "sitemap.xml",
                new { controller = "SiteMapXml", action = "XmlRepresentation" },
                new { method = new HttpMethodConstraint("HEAD") });
        }

        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "ASP.NET MVC requires the method to be an instance member.")]
        public ActionResult XmlRepresentation()
        {
            return new XmlSerializationResult("application/xml", Record.Retrieve<SiteMapUrlSet>(RecordUrn));
        }
    }
}